package com.yyy.instagram;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.timehop.stickyheadersrecyclerview.StickyRecyclerHeadersDecoration;
import com.yyy.instagram.Adaper.GridTweetAdapter;
import com.yyy.instagram.Adaper.UserTweetAdapter;
import com.yyy.instagram.common.PullRefreshHeader.MyPullRefreshFrameLayout;

import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;

/**
 * Created by qqqqqqq on 2016/9/28.
 */

public class SecondFragment extends Fragment {

    RecyclerView recyclerView;

    public static SecondFragment instance() {
        SecondFragment view = new SecondFragment();
        return view;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.second_fragemnt, null);
        final MyPullRefreshFrameLayout ptrFrame = (MyPullRefreshFrameLayout) view.findViewById(R.id.store_search_ptr_frame);
        ptrFrame.setPtrHandler(new PtrHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                frame.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        ptrFrame.refreshComplete();
                    }
                }, 1800);
            }

            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                LinearLayoutManager lm= (LinearLayoutManager) recyclerView.getLayoutManager();
                int topRowVerticalPosition = (recyclerView == null || recyclerView.getChildCount() == 0) ? 0 : recyclerView.getChildAt(0).getTop();
                Log.d("dsad","positon"+topRowVerticalPosition);
                if(lm.findViewByPosition(lm.findFirstVisibleItemPosition()).getTop()==0 && lm.findFirstVisibleItemPosition()==0) {
                    return true;
                }else{
                    return false;
                }
            }
        });

        recyclerView = (RecyclerView) view.findViewById(R.id.display_list);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        GridTweetAdapter adapter = new GridTweetAdapter(getActivity());
        recyclerView.setAdapter(adapter);

        return view;
    }
}
